package com.lys.emsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.emsystem.entity.Student;
import com.lys.emsystem.service.IStudentService;
import com.lys.emsystem.utils.JWTUtils;
import com.lys.emsystem.utils.Result;
import com.lys.emsystem.vo.StudentVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liyansheng
 * @since 2022-04-26
 */
@RestController
@CrossOrigin
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @PostMapping
    public Result getStudent(@RequestBody StudentVo studentVo) {
        IPage<Student> search = studentService.search(new Page(studentVo.getPageIndex(), studentVo.getPageSize()),
                studentVo);
        if (search.getRecords() != null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("list", search.getRecords());
            map.put("totalCount", search.getTotal());
            return Result.buildResult(Result.Status.OK, map);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/add")
    public Result addStudent(@RequestBody Student student) {
        System.out.println(student);
        //默认密码
        if (student.getPassword() == null || student.getPassword().length() == 0) {
            student.setPassword(DigestUtils.md5DigestAsHex(student.getStudentAccount().substring(4).getBytes()));
        }
        //默认性别
        if (student.getSex() == null) {
            student.setSex(1);
        }
        //默认就业状态
        if (student.getEmpStatus() == null) {
            student.setEmpStatus(false);
        }
        if (studentService.save(student)) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }

    }

    @PostMapping("/delete")
    public Result deleteStudent(@RequestBody Student student) {
        if (studentService.removeById(student.getId())) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/update")
    public Result updateStudent(@RequestBody Student student) {
        if (studentService.updateById(student)) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/delList")
    public Result deleteStudents(@RequestBody StudentVo studentVo) {
        if (studentService.removeByIds(Arrays.asList(studentVo.getIds()))) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }


    /**
     * 根据id查询学生信息
     * @param token
     * @return
     */
    @GetMapping
    public Result getStudentById(@RequestHeader("token") String token) {
        if (StringUtils.isEmpty(token)) {
            return Result.buildResult(Result.Status.ERROR,"请先登录！");
        }
        Integer userId = JWTUtils.getUserId(token);
        Student student = studentService.getStudentById(userId);
        return Result.buildResult(Result.Status.OK, student);
    }




}
